<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\BaseController;
use app\admin\model\UserModel;
use app\admin\model\RoleModel;
use app\admin\model\SettingModel;
class Base extends BaseController
{
	protected $admin=[];
	protected function initialize(){
		$route=request()->controller(true)."/".request()->action(true);
		$this->admin=session('admin');
		$this->config=SettingModel::getConfig();
		$this->assign(['config'=>$this->config]);
		//不做验证的控制器/方法
		$notVerfiy=[
			'login/index','login/loginout'
		];

		if(!in_array($route,$notVerfiy) && empty($this->admin)){
			$this->error('请先登录',(string)url('login/index'));
			exit;
		}

		if($route=="login/index" && !empty($this->admin)){
			$this->redirect(url('index/index'));
			exit;
		}

		if(!empty($this->admin)){
			$this->assign(['admin'=>$this->admin]);
		}
		
		if(!empty($this->admin) && request()->controller(true)!='index' && !in_array($route,$notVerfiy)){
			if(!$this->auth()){
				return $this->error('权限不足');
			}
		}
		
	}

	private function auth(){
		$route=request()->controller(true)."/".request()->action(true);
		$routes=RoleModel::getRoleRoute($this->admin['role_id']);

		if(!in_array($route,$routes)){
			return false;
		}

		return true;
	}
    protected function showList($list,int $count=0,int $code=0,string $msg=''){
    	return ['code'=>$code,'msg'=>$msg,'count'=>$count,'data'=>$list];
    }
}
